//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

use proto_gen_rust::board_extdata_api::board_ext_data_api_client::BoardExtDataApiClient;
use proto_gen_rust::board_extdata_api::*;
use tauri::{AppHandle, Emitter, Runtime};

use crate::notice_decode::new_wrong_session_notice;

#[tauri::command]
pub async fn board_extdata_api_cmds_add<R: Runtime>(
    app_handle: AppHandle<R>,
    request: AddRequest,
) -> Result<AddResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardExtDataApiClient::new(chan.unwrap());
    match client.add(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == add_response::Code::WrongSession as i32 {
                if let Err(err) = &app_handle.emit("notice", new_wrong_session_notice("add".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_extdata_api_cmds_update<R: Runtime>(
    app_handle: AppHandle<R>,
    request: UpdateRequest,
) -> Result<UpdateResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardExtDataApiClient::new(chan.unwrap());
    match client.update(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == update_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("update".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_extdata_api_cmds_remove<R: Runtime>(
    app_handle: AppHandle<R>,
    request: RemoveRequest,
) -> Result<RemoveResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardExtDataApiClient::new(chan.unwrap());
    match client.remove(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == remove_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("remove".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_extdata_api_cmds_list<R: Runtime>(
    app_handle: AppHandle<R>,
    request: ListRequest,
) -> Result<ListResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardExtDataApiClient::new(chan.unwrap());
    match client.list(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == list_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("list".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_extdata_api_cmds_get<R: Runtime>(app_handle: AppHandle<R>, request: GetRequest) -> Result<GetResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardExtDataApiClient::new(chan.unwrap());
    match client.get(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == get_response::Code::WrongSession as i32 {
                if let Err(err) = &app_handle.emit("notice", new_wrong_session_notice("get".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}
